const {
    register,
    login,
    update
} = require("../models/userModel")

const jwt = require("jsonwebtoken");
//注册的控制
const regCtr = async (req, res) => {
    // console.log(req.body)
    //注册前要先查询一下,如果有这个用户名了就说注册失败
    let {
        username,
        password,
        userId
    } = req.body;
    if (!username && !password && !userId) {
        res.send({
            status: false,
            code: 0,
            msg: "请正确填写"
        })
        return
    }
    //自动生成userId
    //自动生成0-9 A-Z a-z的的数组
    const uid = getUid();


    req.body.userId= uid

    const check = await login({
        username: req.body.username
    });
    if (check.length) {
        res.send({
            status: false,
            code: 2,
            msg: "用户名重复"
        })
        //然后直接return  结束函数运行
        return
    }



    const result = await register(req.body);
    if (result) {
        res.send({
            status: true,
            code: 1,
            msg: "注册成功"
        })

    } else {
        res.send({
            status: false,
            code: 0,
            msg: "注册失败,必填字段没填"
        })
    }
}

//查询的控制
const loginCtr = async (req, res) => {
    // console.log(req.query)
    let {
        username,
        password
    } = req.body;
    let query = {
        username: username,
        password: password
    }
    // console.log(usn)
    const result = await login(query);
    
    if (result.length) {
        let usn = result[0].username
        let nikename = result[0].nikename
        let uid = result[0].userId
        let info = {
            usn: usn,
            nikename: nikename,
            uid: uid
        } 


        //生成token
        //自定义一个加密字符串
        let sercrect = "MY_NAME_IS_LILEI"
        let token = jwt.sign(info, sercrect, {
            expiresIn: 60 * 5
        })//1.payload 2.sercrect 3,{expiresIn:xx,algorithm:'rs256'}

        res.send({
            status: true,
            code: 1,
            data: info,
            msg: "登陆成功",
            token: token
        })
    } else {
        res.send({
            status: false,
            code: 0,
            msg: "登陆失败"
        })
    }

}


//更新用户信息的接口

const updateCtr = async(req,res)=>{
    let username = req.body.username;
    let info ={
        query:{username:username},
        updated:req.body
    }
    const result =await update(info);
    console.log(result)
    if(result.n){
        res.send({ status: true,
            code: 1,
            msg: "修改成功"})
    }else{
        res.send({ status: true,
            code: 0,
            msg: "修改失败"})
    }
}


module.exports = {
    regCtr,
    loginCtr,
    updateCtr
}

function getUid(){
    //自动生成0-9 A-Z a-z的的数组
var temp ="az"

var arr = [0,1,2,3,4,5,6,7,8,9];
var strArr = []

for(let i = temp.charCodeAt(0);i<=temp.charCodeAt(1);i++){
    var lower =String.fromCharCode(i);
    strArr.push(lower);
    var upper = lower.toUpperCase();
    strArr.push(upper);
}

function getRandomInt(min, max) {
    return parseInt(Math.random() * (max - min + 1)) + min;
  }
 
var arr2 =[]
//要随机生成4位字母
for(let j = 0;j<4;j++){
    var rd = getRandomInt(1,strArr.length);
// console.log(arr[rd])
    arr2.push(strArr[rd])
}

//要随机生成四位数字
var arr3=[]
for(let k = 0;k<4;k++){
    var rd = getRandomInt(1,arr.length-1);
// console.log(arr[rd])
    arr3.push(arr[rd])
}


//...生成一个新的数组
result =[...arr2,...arr3].join("" )
 return result
}